/*******************************************************************************
	WindowEditor
*******************************************************************************/
namespace Mocha {

[GtkTemplate (ui="/com/googlecode/mocha-text-editor/ui/WindowEditor.ui")]
public class WindowEditor : Gtk.ApplicationWindow {
	private const ActionEntry[] actions = {
		{"replace", replace}
	};

	public File? file { get; private set; default = null; }
	[GtkChild]
	private Gtk.SourceView source_view;

	public WindowEditor (Application app, File? infile = null) {
		Object (application: app);
		this.set_size_request (300, 200);
		this.add_action_entries (actions, this);
		
		if (infile != null)
			this.open (infile);
	}
	
	public void open (File infile) {
		try {
			string etag;
			uint8[] cont;
			this.file = infile;
			infile.load_contents (null, out cont, out etag);
			source_view.buffer.text = (string) cont;
		} catch (Error e) {
			warning (e.message);
		}
	}
	
	private void replace () {
		var replace_dialog = new Dialog.Replace (this);
		replace_dialog.run ();
		replace_dialog.destroy ();
	}
}

}
/******************************************************************************/
